package com.youyun56.gps.android.pool;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.youyun56.gps.android.GoodInfoActivity;
import com.youyun56.gps.android.ImsActivity;
import com.youyun56.gps.android.MainTabActivity;
import com.youyun56.gps.android.commands.CommandType;
import com.youyun56.gps.android.common.JSONKey;
import com.youyun56.gps.android.dao.ImDao;
import com.youyun56.gps.android.events.GoodInfoScrollListener;
import com.youyun56.gps.android.pojo.GoodInfo;
import com.youyun56.gps.android.pojo.Im;
import com.youyun56.gps.android.pojo.SearchObject;

public class GoodInfoHander extends Handler {
    private GoodInfoActivity    goodInfoActivity;
    private final static String tag = GoodInfoHander.class.getName();
    
    public GoodInfoHander(GoodInfoActivity goodInfoActivity) {
        this.goodInfoActivity = goodInfoActivity;
    }
    
    @Override
    public void handleMessage(Message msg) {
        JSONObject input = (JSONObject) msg.obj;
        Log.i(tag, input.toString());
        try {
            switch (msg.what) {
                case CommandType.GetGoods:
                    JSONArray getGoodArray = input.getJSONArray(JSONKey.GOODS);
                    for (int i = 0; i < getGoodArray.length(); i++) {
                        JSONObject o = (JSONObject) getGoodArray.get(i);
                        GoodInfo goodInfo = wrapGoodInfo(o);
                        goodInfoActivity.getGoodInfoAdapter().addLast(goodInfo);
                        if (matchSearch(goodInfo)) {
                            goodInfoActivity.getSearchGoodInfoAdapter().addLast(goodInfo);
                        }
                    }
                    if (!input.isNull(JSONKey.IMS)) {
                        List<Im> ims = MainTabActivity.service.getIms();
                        JSONArray array = input.getJSONArray(JSONKey.IMS);
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject o = array.getJSONObject(i);
                            Im im = new Im();
                            im.setAddDay(o.getString("addDay"));
                            im.setAttach(o.getString("attach"));
                            im.setContext(o.getString("context"));
                            im.setUserId(o.getLong("userId"));
                            ims.add(im);
                            ImDao.getInstance().saveIm(im);
                        }
                        Message m = ImsActivity.handler.obtainMessage();
                        m.what = CommandType.ADD_IM;
                        m.obj = input;
                        m.sendToTarget();
                    }
                    goodInfoActivity.getListView().setOnScrollListener(GoodInfoScrollListener.instance);
                    break;
                case CommandType.Append_Good:
                    JSONArray appendGoodArray = input.getJSONArray(JSONKey.GOODS);
                    for (int i = appendGoodArray.length() - 1; i >= 0; i--) {
                        JSONObject o = (JSONObject) appendGoodArray.get(i);
                        GoodInfo goodInfo = wrapGoodInfo(o);
                        goodInfoActivity.getGoodInfoAdapter().addFirst(goodInfo);
                        if (matchSearch(goodInfo)) {
                            goodInfoActivity.getSearchGoodInfoAdapter().addFirst(goodInfo);
                        }
                    }
                    if (!input.isNull(JSONKey.IMS)) {
                        List<Im> ims = MainTabActivity.service.getIms();
                        JSONArray array = input.getJSONArray(JSONKey.IMS);
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject o = array.getJSONObject(i);
                            Im im = new Im();
                            im.setAddDay(o.getString("addDay"));
                            im.setAttach(o.getString("attach"));
                            im.setContext(o.getString("context"));
                            im.setUserId(o.getLong("userId"));
                            ims.add(im);
                            ImDao.getInstance().saveIm(im);
                        }
                        Message m = ImsActivity.handler.obtainMessage();
                        m.what = CommandType.ADD_IM;
                        m.obj = input;
                        m.sendToTarget();
                    }
                    goodInfoActivity.getListView().setOnScrollListener(GoodInfoScrollListener.instance);
                    break;
                case CommandType.Append_Good_END:
                    JSONArray appendGoodEndArray = input.getJSONArray(JSONKey.GOODS);
                    for (int i = 0; i < appendGoodEndArray.length(); i++) {
                        JSONObject o = (JSONObject) appendGoodEndArray.get(i);
                        GoodInfo goodInfo = wrapGoodInfo(o);
                        goodInfoActivity.getGoodInfoAdapter().addLast(goodInfo);
                        if (matchSearch(goodInfo)) {
                            goodInfoActivity.getSearchGoodInfoAdapter().addLast(goodInfo);
                        }
                    }
                    goodInfoActivity.getListView().setOnScrollListener(GoodInfoScrollListener.instance);
                    break;
                case CommandType.Good_Search_END:
                    JSONArray searchEndGoods = input.getJSONArray(JSONKey.GOODS);
                    for (int i = 0; i < searchEndGoods.length(); i++) {
                        JSONObject o = (JSONObject) searchEndGoods.get(i);
                        GoodInfo goodInfo = wrapGoodInfo(o);
                        goodInfoActivity.getGoodInfoAdapter().addLast(goodInfo);
                        if (matchSearch(goodInfo)) {
                            goodInfoActivity.getSearchGoodInfoAdapter().addLast(goodInfo);
                        }
                    }
                    goodInfoActivity.getListView().setOnScrollListener(GoodInfoScrollListener.instance);
                    break;
            }
        } catch (JSONException e) {
            Log.e(tag, e.getMessage(), e);
        }
        
    }
    
    private boolean matchSearch(GoodInfo goodInfo) {
        // TODO Auto-generated method stub
        SearchObject so = goodInfoActivity.getSo();
        if (so == null) {
            return false;
        } else {
            if (so.getToCity() != null && so.getFromCity() != null && so.getToCity() == goodInfo.getToCity() && so.getFromCity() == goodInfo.getFromCity()) {
                return true;
            } else if (so.getFromCity() != null && so.getFromCity() == goodInfo.getFromCity()) {
                return true;
            } else if (so.getToCity() != null && so.getToCity() == goodInfo.getToCity()) {
                return true;
            } else {
                return false;
            }
        }
    }
    
    private GoodInfo wrapGoodInfo(JSONObject o) throws JSONException {
        GoodInfo goodInfo = new GoodInfo();
        goodInfo.setContact(o.getString("contact"));
        goodInfo.setMsg(o.getString("txt"));
        goodInfo.setIndex(o.getInt("index"));
        goodInfo.setAddTime(o.getString("addTime"));
        goodInfo.setFromCity(o.getInt("fromCity"));
        goodInfo.setToCity(o.getInt("toCity"));
        return goodInfo;
    }
}
